<?php
class DeleteController extends Zend_Controller_Action
{
	protected $token;
	
	public function init()
	{
		$title ="delete an object";
		$this->view->__set("headtitle", $title);
			

	}

	public  function preDispatch(){
		
		//token initialazed in the MyobjectsController
		$token = new Zend_Session_Namespace('token');

		$this->token = $token->delete;
	
	}
	

	//@TODO check in all actions that the current user is the one that removes
	public function indexAction()
	{
		$class = $this->_request->getParam("class");
		$id_object = $this->_request->getParam("id_object");
		$name = $this->_request->getParam("name");
		$token = $this->_request->getParam("token");
		
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			// check the token 
			if($token === $this->token)
			{
				if(Application_Model_Enum_ClassName::exist($class) != null){
					$class = Application_Model_Enum_ClassName::exist($class);
					$object = Application_Model_Factory_ObjectMapperFactory::find($id_object, $class);
						
					if($object != null && Application_Model_Tool_ObjectUrl::getObjectNameUrl($object->getName()) == $name)
					{
						$whereObject = array('id_object = ?' => $object->getObjectId());
						$offerMapper = new Application_Model_OfferMapper();
						$offer = $offerMapper->findWhere($whereObject);
						
						$userConnected = new Application_Model_User();
						$userMapper = new Application_Model_UserMapper();
						$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
						$objectOwner = $offer->getUser();
						if($offer != null){
							$whereOffer = array('id_offer = ?' => $offer->getOfferId());
							$offer = $offerMapper->offerBuilder($whereOffer);
							if($offer != null){
								if($userConnected == $objectOwner )
								{
									$classe = get_class($offer);
									$classe = explode("_",$classe);
									$classe = $classe[2];
									
									$getclassId = 'get'.$classe.'Id';
									
									$classMapper = 'Application_Model_'.$classe.'Mapper';
									
									$offerClassMapper = new $classMapper(); 
									$offerClassMapper->delete($offer->$getclassId());
									$getobjectId = 'get'.$class.'Id';
									$objectMapper = 'Application_Model_'.$class.'Mapper';
									$objectMapper = new $objectMapper();
									$objectMapper->delete($object->$getobjectId());
									
									// delete the offer from the index lucene
									$index = Zend_Search_Lucene::open(APPLICATION_PATH . 'search_index');
									Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(1);
									Zend_Search_Lucene_Analysis_Analyzer::setDefault(
									new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive()
									);
									$results = $index->find("offerId:".$offer->getOfferId());
									foreach($results as $result)
									{
										$index->delete($result->id);
									}
									$this->_helper->redirector('index','myobjects');
									
								}
							}
						}
						
					}
				}
				
			}else{
				//TODO handle the error case
				$this->view->msg = 'no';
			}
		}
	}
	
	public function offerAction()
	{
		$id_offer = $this->_request->getParam("id_offer");
		$token = $this->_request->getParam("token");
	
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			// check the token
			if($token === $this->token)
			{
				
						$offerMapper = new Application_Model_OfferMapper();
						if(is_numeric($id_offer))
						{
							$offer = $offerMapper->findWhere(array('id_offer = ?' => $id_offer));
							$userConnected = new Application_Model_User();
							$userMapper = new Application_Model_UserMapper();
							$userConnected = $userMapper->find($auth->getIdentity ()->id_user);
							$objectOwner = $offer->getUser();
							if($offer != null){
								$whereOffer = array('id_offer = ?' => $offer->getOfferId());
								$offer = $offerMapper->offerBuilder($whereOffer);
								if($offer != null){
									if($userConnected == $objectOwner )
									{
										$classe = get_class($offer);
										$classe = explode("_",$classe);
										$classe = $classe[2];
											
										$getclassId = 'get'.$classe.'Id';
											
										$classMapper = 'Application_Model_'.$classe.'Mapper';
											
										$offerClassMapper = new $classMapper();
										$offerClassMapper->deleteOfferOnly($offer->$getclassId());
										
											
										// delete the offer from the index lucene
										$index = Zend_Search_Lucene::open(APPLICATION_PATH . 'search_index');
										Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(1);
										Zend_Search_Lucene_Analysis_Analyzer::setDefault(
										new Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive()
										);
										$results = $index->find("offerId:".$offer->getOfferId());
										foreach($results as $result)
										{
											$index->delete($result->id);
										}
										$this->_helper->redirector('index','myobjects');
											
								
									}
				
								}
							}
						}
	
			}else{
				//TODO handle the error case
				$this->view->msg = 'no';
			}
		}
	}
	
	public function requestAction()
	{
		$id_request = $this->_request->getParam("id_request");
		$token = $this->_request->getParam("token");
		
		$auth = Zend_Auth::getInstance ();
		if ($auth->hasIdentity ()) {
			// check the token
			if($token === $this->token)
			{
				$reqMapper = new Application_Model_RequestMapper();
				$request = null;
				$request = $reqMapper->find($id_request, new Application_Model_Request());
				if($request != null){
					$reqMapper->delete($id_request);
				}
				
				$this->_helper->redirector('index','index');
							
			}else{
				//TODO handle the error case
				$this->view->msg = 'no';
			}
		}
	}
}